﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE
{
    public partial class cbchon : DevExpress.XtraEditors.XtraForm
    {
        DAL.EntitiesDAL.ban_DAL _dal = new DAL.EntitiesDAL.ban_DAL();
        DAL.EntitiesDAL.chitietphieuDAL _dll = new DAL.EntitiesDAL.chitietphieuDAL();
        BLL.EntitiesBLL.phieuBLL _bll = new BLL.EntitiesBLL.phieuBLL();
        DAL.EntitiesDAL.savingDAL _save = new DAL.EntitiesDAL.savingDAL();
        DAL.EntitiesDAL.phieu_DAL _dall = new DAL.EntitiesDAL.phieu_DAL();
        DAL.EntitiesDAL.chitietphieuDAL _ttt = new DAL.EntitiesDAL.chitietphieuDAL();
        DAL.EntitiesDAL.savingDAL _sav = new DAL.EntitiesDAL.savingDAL();

        public cbchon()
        {
            InitializeComponent();
        }
        protected DataTable _dtb = new DataTable("Tmp");
        protected DataTable _okey = new DataTable("bang");

        private void frmGDchinh_Load(object sender, EventArgs e)
        {
            dtNgay.Text = DateTime.Now.ToString().Trim();
            lbctienchu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtTongtien.Text));
            DataTable _nv = _ttt.tenNV();
            cboNV.Properties.DataSource = _nv;

            txtTongtien.Text = "0";
            DataTable _DDL = _dll.load_khuvuc();
            cbKV.Properties.DataSource = _DDL;
            //load loai mon
            DataTable _loai = _dal.Select_load();
            cbLoaimon.Properties.DataSource = _loai;
            //load ban chua tao phieu
            DataTable _ban = _dal.BAN();
            cbBan.Properties.DataSource = _ban;

            DataTable _ban1 = _dal.BAN();
            cbBandcdoi.Properties.DataSource = _ban1;

            DataTable _load = _dal.Select_tten();
            cbMP2.Properties.DataSource = _load;

            DataTable _load2 = _dal.Select_tten();
            cbMP1.Properties.DataSource = _load2;

            DataTable _ok = _dll.load_monhuy(txtSTT.Text);
            cbMonhuy.Properties.DataSource = _ok;
            string aaaaa = _sav.tong_thu_ht(dtNgay.EditValue + "");
            txtTongthuht.Text = aaaaa;


            //txttienchu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtTongthuht.Text));
            //load ban  tao phieu
            //DataTable _banok = _dal.BANok();
            //cbBanchon.Properties.DataSource = _banok;
            //load ban

            //lvBan.Items.Clear();
            //string id = "kv01";
            //_dtb = _dal.Select_KV(id);
            //DataTable dlll = _dal.load();

            //foreach (DataRow drw in _dtb.Rows)
            //{
            //    //luu.Text = drw["maban"].ToString();
            //    ListViewItem lvwitem = new ListViewItem();
            //    ListViewItem lvwitem2 = new ListViewItem();
            //    lvwitem.Text = drw[1].ToString();
            //    lvwitem.Tag = drw[0].ToString();
            //    int hinh = 0;

            //    foreach (DataRow row in dlll.Rows)
            //    {

            //        //labelControl2.Text = row["maban"] + "";
            //        if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
            //        {
            //            lvwitem.ImageIndex = 1;
            //            hinh = 1;
            //        }
            //        //else
            //        //{
            //        //    lvwitem.ImageIndex = 0;
            //        //    hinh = 0;

            //        //}
            //    }
            //    if (hinh == 0)
            //        lvwitem.ImageIndex = 0;
            //    lvBan.Items.Add(lvwitem);
            //}
            /////

            //if (txtTongtien.Text == null)
            //{
            //    txtTongtien.Text = "0";
            //}
        }

        private void txtTongtien_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                lbctienchu.Text = "(" + UTL.Currency.ToString(Convert.ToDecimal(txtTongtien.Text)) + ")";
                string _tong = _sav.tong_thu_ht(dtNgay.EditValue + "");
                txtTongthuht.Text = _tong;
                if(_tong == null)
                {
                    txtTongthuht.Text = "0";
                }
                txttienchu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtTongthuht.Text));
            }
            catch
            {
               
            }
        }

        void load_tt()
        {
            lvBan.Items.Clear();
            _dtb = _dal.Select_KV(cbKV.EditValue + "");
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {
                //luu.Text = drw["maban"].ToString();
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;

                foreach (DataRow row in dlll.Rows)
                {

                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                    }
                    //else
                    //{
                    //    lvwitem.ImageIndex = 0;
                    //    hinh = 0;

                    //}
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }


        void loadban()
        {
            //lvwTDofBan.DataBindings.Clear();
            //lvBan.DataBindings.Clear();
            lvBan.Items.Clear();
            _dtb = _dal.Select_KV(cbKV.EditValue + "");
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {
                //luu.Text = drw["maban"].ToString();
                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
                foreach (DataRow row in dlll.Rows)
                {
                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                        //luu.Text = row["maban"].ToString();
                    }
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                lvBan.Items.Add(lvwitem);
            }
        }

        private void labelControl8_Click(object sender, EventArgs e)
        {

        }

        private void cbKV_EditValueChanged(object sender, EventArgs e)
        {
            lvBan.Items.Clear();
            loadban();
        }

        private void lvBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            _dtb = _dal.Select_KV(cbKV.EditValue + "");
            DataTable dlll = _dal.load();

            foreach (DataRow drw in _dtb.Rows)
            {


                ListViewItem lvwitem = new ListViewItem();
                ListViewItem lvwitem2 = new ListViewItem();
                lvwitem.Text = drw[1].ToString();
                lvwitem.Tag = drw[0].ToString();
                int hinh = 0;
                foreach (DataRow row in dlll.Rows)
                {
                    //labelControl2.Text = row["maban"] + "";
                    if (row["maban"].ToString() == lvwitem.Tag.ToString() && row["tinhtien"].ToString() != null)
                    {
                        lvwitem.ImageIndex = 1;
                        hinh = 1;
                        //luu.Text = row["maban"].ToString();
                    }
                    //luu.Text = lvwitem.Tag.ToString();
                }
                if (hinh == 0)
                    lvwitem.ImageIndex = 0;
                //lvBan.Items.Add(lvwitem);
                //luu.Text = drw["maban"].ToString();
            }
        }

        private void cbBan_EditValueChanged(object sender, EventArgs e)
        {
            frmGDchinh_Load(sender, e);
            DataTable _ma = _dall.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
                    txtSTT.Text = ma;
                }
        }

        private void cmdTaoban_Click(object sender, EventArgs e)
        {
            txtTongtien.Text = "0";
            var o = new DAL.Entities.phieu()
            {
                MAPHIEU = ten1.Text,
                TINHTIEN = txtTongtien.Text,
                NGAYTAOPHIEU = Convert.ToDateTime(dtNgay.EditValue + ""),
                MABAN = maban.Text,
            };

            var oki = _dall.Insert(o);
            if (oki)
            {
                MessageBox.Show("Đã tạo bàn thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSTT.Text = ten1.Text;
            }
            else
            {
                MessageBox.Show("Lỗi! Tạo bàn thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            load_tt();
        }

        private void cbLoaimon_EditValueChanged(object sender, EventArgs e)
        {
            _okey = _dal.thucdon_loaimon(cbLoaimon.EditValue + "");
            dsThucdon.DataSource = _okey;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string id = ten1.Text;

            DialogResult result;
            var ok = _dall.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    MessageBox.Show("Xóa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //cbBanchon.Text = null;
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            load_tt();
        }

        private void cbBanchon_EditValueChanged(object sender, EventArgs e)
        {
            frmGDchinh_Load(sender, e);
        }

        void ClearDataBindings()
        {
            txtmamon.DataBindings.Clear();
            txttenmon.DataBindings.Clear();
            txtdongia.DataBindings.Clear();
           
        }

        void DataBindingControl()
        {
           
                txtmamon.DataBindings.Add("EditValue", _okey, ".MAMON");
                txttenmon.DataBindings.Add("EditValue", _okey, ".TENMON");
                txtdongia.DataBindings.Add("EditValue", _okey, ".DONGIA");
           
           
           
        }

        void load_dsmon()
        {
            try
            {
                //string xx, yy, zz = null;

                maban.DataBindings.Clear();
                lvwTDofBan.Items.Clear();
                _dtb = _dal.Select();
                ListViewItem lvwitem = new ListViewItem();
                lvwitem = lvBan.SelectedItems[lvBan.SelectedItems.Count - 1];
                if (lvwitem.ImageIndex == 1 || lvwitem.ImageIndex == 0)
                {
                    foreach (DataRow drw in _dtb.Rows)
                    {
                        //xx = drw["maban"].ToString();

                        if (drw["MaBan"].ToString() == lvwitem.Tag.ToString())
                        {


                            DataTable _hien = _dal.hthi(drw["MABAN"].ToString());

                            lvwTDofBan.Columns.Clear();
                            lvwTDofBan.Clear();
                            lvwTDofBan.Columns.Add("Tên món");
                            lvwTDofBan.Columns[0].Width = 80;
                            lvwTDofBan.Columns.Add("Đơn giá");
                            lvwTDofBan.Columns[1].Width = 90;
                            lvwTDofBan.Columns.Add("Số lượng");
                            //lvwTDofBan.Columns[2].Width = 70;
                            //lvwTDofBan.Columns.Add("Tính tiền");
                            lvwTDofBan.Columns[2].Width = 80;
                            lvwTDofBan.Columns.Add("Giảm giá (%)");
                            lvwTDofBan.Columns[3].Width = 70;

                            int Tong = 0;

                            foreach (DataRow row1 in _hien.Rows)
                            {

                                if (row1["tinhtien"].ToString() != null || row1["tinhtien"].ToString() == "0.00")
                                {
                                    //yy = row1["maphieu"].ToString();
                                    ListViewItem lvwitem1 = new ListViewItem();
                                    lvwitem1.Text = row1["TenMon"].ToString();
                                    lvwitem1.SubItems.Add(row1["DonGia"].ToString());
                                    lvwitem1.SubItems.Add(row1["SoLuong"].ToString());
                                    //lvwitem1.SubItems.Add(row1["tinhtien"].ToString());
                                    lvwitem1.SubItems.Add(row1["GiamGia"].ToString());

                                    lvwTDofBan.Items.Add(lvwitem1);

                                    //maban.DataBindings.Add("EditValue", _dtb, ".maban");
                                    Tong += ((Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"])) - (Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"]) * Convert.ToInt32(row1["GiamGia"]) / 100));
                                }
                                else
                                { }


                                txtTongtien.Text = Tong.ToString();


                            }
                            //maban.Text = xx + "";
                            //txtSTT.Text = yy + "";
                        }
                    }
                }
            }
            catch
            {

            }
        }

        void load_dsmonchon()
        {
            try
            {
                string xx, yy = null;
                string zz = null;

                maban.DataBindings.Clear();
                lvwTDofBan.Items.Clear();
                _dtb = _dal.Select();
                ListViewItem lvwitem = new ListViewItem();
                lvwitem = lvBan.SelectedItems[lvBan.SelectedItems.Count - 1];
                if (lvwitem.ImageIndex == 1 || lvwitem.ImageIndex == 0)
                {
                    foreach (DataRow drw in _dtb.Rows)
                    {
                        xx = drw["maban"].ToString();

                        if (drw["MaBan"].ToString() == lvwitem.Tag.ToString())
                        {


                            DataTable _hien = _dal.hthi(drw["MABAN"].ToString());

                            lvwTDofBan.Columns.Clear();
                            lvwTDofBan.Clear();
                            lvwTDofBan.Columns.Add("Tên món");
                            lvwTDofBan.Columns[0].Width = 80;
                            lvwTDofBan.Columns.Add("Đơn giá");
                            lvwTDofBan.Columns[1].Width = 90;
                            lvwTDofBan.Columns.Add("Số lượng");
                            //lvwTDofBan.Columns[2].Width = 70;
                            //lvwTDofBan.Columns.Add("Tính tiền");
                            lvwTDofBan.Columns[2].Width = 80;
                            lvwTDofBan.Columns.Add("Giảm giá (%)");
                            lvwTDofBan.Columns[3].Width = 70;

                            int Tong = 0;

                            foreach (DataRow row1 in _hien.Rows)
                            {

                                if (row1["tinhtien"].ToString() != null || row1["tinhtien"].ToString() == "0.00")
                                {
                                    yy = row1["maphieu"].ToString();
                                    zz = row1["SoLuong"].ToString();
                                    ListViewItem lvwitem1 = new ListViewItem();
                                    lvwitem1.Text = row1["TenMon"].ToString();
                                    lvwitem1.SubItems.Add(row1["DonGia"].ToString());
                                    lvwitem1.SubItems.Add(row1["SoLuong"].ToString());
                                    //lvwitem1.SubItems.Add(row1["tinhtien"].ToString());
                                    lvwitem1.SubItems.Add(row1["GiamGia"].ToString());

                                    lvwTDofBan.Items.Add(lvwitem1);

                                    //maban.DataBindings.Add("EditValue", _dtb, ".maban");
                                    Tong += ((Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"])) - (Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"]) * Convert.ToInt32(row1["GiamGia"]) / 100));
                                }
                                else
                                { }


                                txtTongtien.Text = Tong.ToString();


                            }
                            cbBan.Text = xx + "";
                            sl.Text = zz + "";
                            txtSTT.Text = yy + "";
                        }
                    }
                }
            }
            catch
            {

            }
        }

        private void lvBan_Click(object sender, EventArgs e)
        {
            lbctienchu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtTongtien.Text));
            if(txtTongtien.Text == null)
            {
                txtTongtien.Text = "0";
            }
            try
            {
                

                string xx, yy= null;
                string zz = null;
                DataTable _ma = _dall.Select_max();
                if (_ma != null)
                    if (_ma.Rows.Count > 0 || Convert.ToInt32(txtSTT.Text) == 0)
                    {
                        string ma = _bll.AddID(_ma.Rows[0]["id"] + "");
                        ten1.Text = ma;
                    }
                maban.DataBindings.Clear();
                lvwTDofBan.Items.Clear();
                _dtb = _dal.Select();
                ListViewItem lvwitem = new ListViewItem();
                lvwitem = lvBan.SelectedItems[lvBan.SelectedItems.Count - 1];
                if (lvwitem.ImageIndex == 1 || lvwitem.ImageIndex == 0)
                {
                    foreach (DataRow drw in _dtb.Rows)
                    {
                        xx = drw["maban"].ToString();
                        //txtTongtien.Text = "0";
                        if (drw["MaBan"].ToString() == lvwitem.Tag.ToString())
                        {


                            DataTable _hien = _dal.hthi(drw["MABAN"].ToString());

                            lvwTDofBan.Columns.Clear();
                            lvwTDofBan.Clear();
                            lvwTDofBan.Columns.Add("Tên món");
                            lvwTDofBan.Columns[0].Width = 80;
                            lvwTDofBan.Columns.Add("Đơn giá");
                            lvwTDofBan.Columns[1].Width = 90;
                            lvwTDofBan.Columns.Add("SL");
                            //lvwTDofBan.Columns[2].Width = 70;
                            //lvwTDofBan.Columns.Add("Tính tiền");
                            lvwTDofBan.Columns[2].Width = 50;
                            lvwTDofBan.Columns.Add("Giảm giá (%)");
                            lvwTDofBan.Columns[3].Width = 80;

                            int Tong = 0;

                            foreach (DataRow row1 in _hien.Rows)
                            {

                                if (row1["tinhtien"].ToString() != null)
                                {
                                    yy = row1["maphieu"].ToString();
                                    zz = row1["SoLuong"].ToString();
                                    ListViewItem lvwitem1 = new ListViewItem();
                                    lvwitem1.Text = row1["TenMon"].ToString();
                                    lvwitem1.SubItems.Add(row1["DonGia"].ToString());
                                    lvwitem1.SubItems.Add(row1["SoLuong"].ToString());
                                    //lvwitem1.SubItems.Add(row1["tinhtien"].ToString());
                                    lvwitem1.SubItems.Add(row1["GiamGia"].ToString());

                                    lvwTDofBan.Items.Add(lvwitem1);

                                    //maban.DataBindings.Add("EditValue", _dtb, ".maban");
                                    Tong += ((Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"])) - (Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"]) * Convert.ToInt32(row1["GiamGia"]) / 100));
                                }
                                else
                                { }


                                txtTongtien.Text = Tong.ToString();


                            }
                            maban.Text = xx + "";
                            txtbandinhdoi.Text = xx + "";
                            //cbBan.Text = xx + "";
                            sl.Text = zz + "";
                            txtSTT.Text = yy + "";
                            
                            //txtMP1.Text = yy + "";
                            if (txtSTT.Text == "")
                            {
                                string _ddd = _dall.Select_stt(maban.Text);
                                txtSTT.Text = _ddd;
                            }
                            //DataTable ktra = _dll.check_chitietphieu(txtSTT.Text, txtmamon.Text);
                            //if(ktra.Rows.Count ==0)
                            //{
                            //    txtTongtien.Text = "0";
                            //}
                        }
                    }
                }
            }
            catch
            {

            }
        }

        private void dsThucdon_Click(object sender, EventArgs e)
        {
            ClearDataBindings();
            DataBindingControl();
            //cbLoaimon_EditValueChanged( sender,  e);
        }

        private void dsThucdon_DoubleClick(object sender, EventArgs e)
        {
            //int aa = Convert.ToInt32(sl.Text);
            //ClearDataBindings();
            DataTable _bang;
             _bang = _dll.check_chitietphieu(txtSTT.Text, txtmamon.Text);
             string kt = _dll.ktra(txtSTT.Text, txtmamon.Text);

            

            if(_bang.Rows.Count ==0)
            {
            var o = new DAL.Entities.chitietphieu()
            {
                MAPHIEU = txtSTT.Text,
                MAMON = txtmamon.Text,
                MANV = cboNV.EditValue + "",
                SOLUONG = 1,
                DONGIA = txtdongia.Text,
                GIAMGIA = cbGG.EditValue + "",

                TINHTIEN = txtdongia.Text,
            };

            var oki = _dll.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    load_dsmonchon();
                    load_dsmon();
                    ClearDataBindings();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
                }
            }
            else
            {
                string xx = kt.ToString();
                sl.Text = xx;
                var o = new DAL.Entities.chitietphieu()
                {
                MAPHIEU = txtSTT.Text,
                MAMON = txtmamon.Text,
                MANV = cboNV.EditValue + "",
                SOLUONG = Convert.ToInt32(sl.Text) + 1,
                DONGIA = txtdongia.Text,
                GIAMGIA = cbGG.EditValue + "",

                TINHTIEN = txtdongia.Text,
                };

                var oki = _dll.Update(o);
                if (oki)
                {
                    MessageBox.Show("cap nhat lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    load_dsmonchon();
                    //load_dsmon();
                    //lvBan_SelectedIndexChanged( sender,  e);
                }
                else
                {
                    MessageBox.Show("Lỗi! không cập nhật dc! thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    load_dsmonchon();
                }
            }
        }

        private void cmdThanhtoan_Click(object sender, EventArgs e)
        {
            //thanh toan
            var o = new DAL.Entities.phieu()
            {
                MAPHIEU = txtSTT.Text,
                TINHTIEN = txtTongtien.Text,
                NGAYTAOPHIEU = Convert.ToDateTime(dtNgay.EditValue),
                MABAN = maban.Text,
            };

            

            var oki = _dall.Update(o);
           
            if (oki)
            {
                MessageBox.Show("Đã Thanh toán xong! Vui lòng chờ để nhận hóa đơn!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                var u = new DAL.Entities.saving()
                {
                    MAPHIEU = txtSTT.Text,
                    TINHTIEN = txtTongtien.Text,
                    NGAYTAOPHIEU = Convert.ToDateTime(dtNgay.EditValue + ""),
                    MABAN = maban.Text,
                };
                var okey = _save.Insert(u);
                if (okey)
                {
                    MessageBox.Show("Okkkkkkkkkkkkkkkkkkk", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                

            }
            else
            {
                MessageBox.Show("Bàn này không có gọi món nao!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            //break();
            load_tt();
            txtTongtien.Text = "0";
            lvwTDofBan.DataBindings.Clear();
            var stt = txtSTT.Text;

            var frm = new Reports.frmbase();
            var xr = new Reports.baocao();

            xr.DataSource = _dall.Select_Report(stt);
            frm.SetReport(xr);
            frm.Show();
            string x = txtSTT.Text;
            var id = _dall.xoa(x);
            if (id)
            {
                MessageBox.Show("Xóa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                load_tt();
            }
        }

        private void lvwTDofBan_Click(object sender, EventArgs e)
        {
            


                string xx, yy = null;
                string zz = null;

                maban.DataBindings.Clear();
                lvwTDofBan.Items.Clear();
                _dtb = _dal.Select();
                ListViewItem lvwitem = new ListViewItem();
                lvwitem = lvBan.SelectedItems[lvBan.SelectedItems.Count - 1];
                if (lvwitem.ImageIndex == 1 || lvwitem.ImageIndex == 0)
                {
                    foreach (DataRow drw in _dtb.Rows)
                    {
                        xx = drw["maban"].ToString();

                        if (drw["MaBan"].ToString() == lvwitem.Tag.ToString())
                        {


                            DataTable _hien = _dal.hthi(drw["MABAN"].ToString());

                            lvwTDofBan.Columns.Clear();
                            lvwTDofBan.Clear();
                            lvwTDofBan.Columns.Add("Tên món");
                            lvwTDofBan.Columns[0].Width = 80;
                            lvwTDofBan.Columns.Add("Đơn giá");
                            lvwTDofBan.Columns[1].Width = 90;
                            lvwTDofBan.Columns.Add("Số lượng");
                            //lvwTDofBan.Columns[2].Width = 70;
                            //lvwTDofBan.Columns.Add("Tính tiền");
                            lvwTDofBan.Columns[2].Width = 80;
                            lvwTDofBan.Columns.Add("Giảm giá (%)");
                            lvwTDofBan.Columns[3].Width = 70;

                            int Tong = 0;

                            foreach (DataRow row1 in _hien.Rows)
                            {
                                //ten.Text = row1["TenMon"].ToString();
                                if (row1["tinhtien"].ToString() != null)
                                {
                                    
                                    yy = row1["maphieu"].ToString();
                                    zz = row1["SoLuong"].ToString();
                                    ListViewItem lvwitem1 = new ListViewItem();
                                    lvwitem1.Text = row1["TenMon"].ToString();
                                    lvwitem1.SubItems.Add(row1["DonGia"].ToString());
                                    lvwitem1.SubItems.Add(row1["SoLuong"].ToString());
                                    //lvwitem1.SubItems.Add(row1["tinhtien"].ToString());
                                    lvwitem1.SubItems.Add(row1["GiamGia"].ToString());

                                    lvwTDofBan.Items.Add(lvwitem1);
                                    
                                    //maban.DataBindings.Add("EditValue", _dtb, ".maban");
                                    Tong += ((Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"])) - (Convert.ToInt32(row1["DonGia"]) * Convert.ToInt32(row1["SoLuong"]) * Convert.ToInt32(row1["GiamGia"]) / 100));
                                }
                                else
                                { }

                                
                                txtTongtien.Text = Tong.ToString();


                            }
                            
                            maban.Text = xx + "";
                            //cbBan.Text = xx + "";
                            sl.Text = zz + "";
                            txtSTT.Text = yy + "";
                            if (txtSTT.Text == "")
                            {
                                string _ddd = _dall.Select_stt(maban.Text);
                                txtSTT.Text = _ddd;
                            }
                        
                    }
                }
            }
           
        }

        private void ten_Click(EventArgs e)
        {

        }

        private void dockPanel12_Click(object sender, EventArgs e)
        {

        }

        private void labelControl9_Click(object sender, EventArgs e)
        {

        }

        private void cmđoiban_Click(object sender, EventArgs e)
        {
            string ban = cbBandcdoi.EditValue + "";
            string mk = txtSTT.Text;
            
            var  xxx = _dall.doi_ban(ban, mk);
            if (xxx)
            {
                MessageBox.Show("Đã đổi bàn thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTongtien.Text = "0";
            }
            else
            {
                MessageBox.Show("Lỗi! Đổi bàn thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            load_tt();
        }

        private void cbMP1_EditValueChanged(object sender, EventArgs e)
        {
            string p1 = _dall.Select_stt(cbMP1.EditValue + "");
            txtP1.Text = p1;
        }

        private void cbMP2_EditValueChanged(object sender, EventArgs e)
        {
            string p2 = _dall.Select_stt(cbMP2.EditValue + "");
            txtP2.Text = p2;
        }

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            string ban = txtP1.Text;

            string mk = txtP2.Text;

            var xxx = _dall.gop_ban(ban, mk);
            if (xxx)
            {
                MessageBox.Show("Đã gộp bàn thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                load_tt();
                txtTongtien.Text = "0";
            }
            else
            {
                MessageBox.Show("Lỗi! gộp bàn thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            load_tt();
        }

        private void cmdGM_Click(object sender, EventArgs e)
        {
            ClearDataBindings();
            DataTable _bang;
            _bang = _dll.check_chitietphieu(txtSTT.Text, txtmamon.Text);
            string kt = _dll.ktra(txtSTT.Text, txtmamon.Text);



            if (_bang.Rows.Count == 0)
            {
                var o = new DAL.Entities.chitietphieu()
                {
                    MAPHIEU = txtSTT.Text,
                    MAMON = txtmamon.Text,
                    MANV = cboNV.EditValue + "",
                    SOLUONG = 1,
                    DONGIA = txtdongia.Text,
                    GIAMGIA = cbGG.EditValue + "",

                    TINHTIEN = txtdongia.Text,
                };

                var oki = _dll.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    load_dsmonchon();
                    load_dsmon();
                    ClearDataBindings();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            else
            {
                string xx = kt.ToString();
                sl.Text = xx;
                var o = new DAL.Entities.chitietphieu()
                {
                    MAPHIEU = txtSTT.Text,
                    MAMON = txtmamon.Text,
                    MANV = cboNV.EditValue + "",
                    SOLUONG = Convert.ToInt32(sl.Text) + 1,
                    DONGIA = txtdongia.Text,
                    GIAMGIA = cbGG.EditValue + "",

                    TINHTIEN = txtdongia.Text,
                };

                var oki = _dll.Update(o);
                if (oki)
                {
                    MessageBox.Show("cap nhat lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    load_dsmonchon();
                    //load_dsmon();
                    //lvBan_SelectedIndexChanged( sender,  e);
                }
                else
                {
                    MessageBox.Show("Lỗi! không cập nhật dc! thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    load_dsmonchon();
                }
            }
        }

        private void cbMonhuy_EditValueChanged(object sender, EventArgs e)
        {
            //cmdLoad_Click( sender,  e);
            //txtSLhuy.Text = "";
            //txtSLhuy.DataBindings.Clear();
            string _ddd = _dll.Select_tenmon(cbMonhuy.EditValue + "", txtSTT.Text);
            txtSLhuy.Text = _ddd;
        }

        private void cmdLoad_Click(object sender, EventArgs e)
        {
            cbMonhuy.Text = "";
            cbMonhuy.DataBindings.Clear();
            DataTable _ok = _dll.load_monhuy(txtSTT.Text);
            cbMonhuy.Properties.DataSource = _ok;
            //txtSLhuy.Text = "";
        }

        private void cmdHuygoi_Click(object sender, EventArgs e)
        {
            if(Convert.ToInt32(txtSLhuy.Text) > 1)
            {
            //lvBan_Click(sender, e);
            string mon = cbMonhuy.EditValue + "";
            string mk = txtSTT.Text;
            string sl = (Convert.ToInt32(txtSLhuy.Text) - 1).ToString();

            var xxx = _dll.doi_soluong(sl, mk, mon);
            if (xxx)
            {
                MessageBox.Show("Đã huy bàn thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                load_tt();
                lvBan_Click( sender,  e);
                cmdLoad_Click( sender,  e);
            }
            else
            {
                MessageBox.Show("Lỗi! huy bàn thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            load_tt();
            }
            else
            {
                MessageBox.Show("Bạn không thể hũy chọn được!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtTongthuht_EditValueChanged(object sender, EventArgs e)
        {
            lbctienchu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtTongtien.Text));
        }

        private void textEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(1==1)
            {

            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if(radioButton1.Checked == true)
            {
                string _tongkt = _sav.tong_thu_ht(dtNgay.EditValue + "");
                txtTongloi.Text = _tongkt;
                txtloinhuan.Text = (Convert.ToDecimal(txtTongloi.Text) - Convert.ToDecimal(txtTienchi.Text)).ToString();
                txttongloichu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtloinhuan.Text));
            }
            if (radioButton2.Checked == true)
            {
                string _tongkt = _sav.loinhuan_thang(dtNgay.EditValue + "");
                txtTongloi.Text = _tongkt;
                txtloinhuan.Text = (Convert.ToDecimal(txtTongloi.Text) - Convert.ToDecimal(txtTienchi.Text)).ToString();
                txttongloichu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtloinhuan.Text));
            }
            if (radioButton3.Checked == true)
            {
                string _tongkt = _sav.loinhuan_nam(dtNgay.EditValue + "");
                txtTongloi.Text = _tongkt;
                txtloinhuan.Text = (Convert.ToDecimal(txtTongloi.Text) - Convert.ToDecimal(txtTienchi.Text)).ToString();
                txttongloichu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtloinhuan.Text));
            }
        }

        private void dtNgay_EditValueChanged_1(object sender, EventArgs e)
        {
            //string aaaaa = _sav.tong_thu_ht(dtNgay.EditValue + "");
            //txtTongthuht.Text = aaaaa;


            //txttienchu.Text = UTL.Currency.ToString(Convert.ToDecimal(txtTongthuht.Text));
        }
    }
}